import axios from "axios";
import store from "./store/index"

// 创建axios实例
const instance = axios.create({
    baseURL: ' https://mallapi.duyiedu.com/',
});

// 添加请求拦截器
// 添加请求拦截器
instance.interceptors.request.use((config) => {
    // 在发送请求之前做些什么
    if (config.url.includes("/passport")) {
        return config;
    } else {
        return {
            ...config,
            params: {
                ...config.params,
                appkey: store.state.user.appkey
            }
        }
    }
}, (error) => Promise.reject(error));


// 添加响应拦截器
instance.interceptors.response.use((response) => {
    // console.log(response)
    if (response.data.status === 'fail') { // 失败的话
        return Promise.reject(response.data.msg);
    } else {
        return response.data.data;
    }
}, (error) => Promise.reject(error));


export default instance;
